package cn.shuangliang.contract.service;

import cn.shuangliang.contract.vo.Constant;
import cn.shuangliang.contract.dao.ProductMapper;
import cn.shuangliang.contract.model.Product;
import cn.shuangliang.contract.util.MyBatisUtil;
import cn.shuangliang.contract.vo.Page;

import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class ProductService {
    public int add(Product p){
        try {
            ProductMapper  mapper = MyBatisUtil.get().getMapper(ProductMapper.class);
            return mapper.insertSelective(p);
        } finally {
            MyBatisUtil.close();
        }
    }
    public int update(Product p){
        try {
            ProductMapper  mapper = MyBatisUtil.get().getMapper(ProductMapper.class);
            return mapper.updateByPrimaryKey(p);
        } finally {
            MyBatisUtil.close();
        }
    }
    public Product get(Long id){
        try {
            ProductMapper  mapper = MyBatisUtil.get().getMapper(ProductMapper.class);
            return mapper.selectByPrimaryKey(id);
        } finally {
            MyBatisUtil.close();
        }
    }
    public Page<Product> search(  int pageNo,    String pname,  String type){

        try {
            ProductMapper  mapper = MyBatisUtil.get().getMapper(ProductMapper.class);
            List<Product> search = mapper.search(pageNo, Constant.pageSize , pname, type);
            int total = mapper.searchTotal( pname, type);
            return new Page<Product>(search,pageNo,total, Constant.pageSize );
        } finally {
            MyBatisUtil.close();
        }
    }

    public Page<Product> search(  int pageNo,int pageSize,    String pname,  String type){

        try {
            ProductMapper  mapper = MyBatisUtil.get().getMapper(ProductMapper.class);
            List<Product> search = mapper.search(pageNo, pageSize , pname, type);
            int total = mapper.searchTotal( pname, type);
            return new Page<Product>(search,pageNo,total, Constant.pageSize );
        } finally {
            MyBatisUtil.close();
        }
    }

    public int del(Long id) {


        try {
            ProductMapper  mapper = MyBatisUtil.get().getMapper(ProductMapper.class);
            return  mapper.deleteByPrimaryKey(id);
        } finally {
            MyBatisUtil.close();
        }
    }
}
